/**	
 * <br>
 * Copyright 2014 om.All rights reserved.<br>
 * <br>			 
 * Package: com.web.controller <br>
 * FileName: HelloController.java <br>
 * <br>
 * @version
 * @author Liuxingwen
 * @created 2016��9��3��
 * @last Modified 
 * @history
 */
package com.hulk.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hulk.common.aspect.SystemControllerLog;
import com.hulk.model.HelloWorld;
import com.hulk.service.HelloWorldService;
import com.hulk.utils.RandomUtil;

/**
 * 
 * {该处说明该构造函数的含义及作用}
 * 
 * @author Liuxingwen
 * @created 2016年9月5日 下午8:41:59
 * @lastModified
 * @history
 */
@Controller
@Scope("prototype")
@RequestMapping({ "/hello", "/" })
public class HelloController {
	@SuppressWarnings("unused")
	private static Logger logger = Logger.getLogger(HelloController.class);

	@Resource
	private HelloWorldService helloWorldService;
//	@Resource
//	private DubboDemosService dubboDemosService;

//	// http://127.0.0.1:8089/dubbo-web/hello/redisdemo.html
//	@ResponseBody
//	@RequestMapping(value = "/redisdemo.html", method = RequestMethod.GET)
//	public String redisDemo(HttpServletRequest request) throws Exception {
//		JSONObject jsonObject = new JSONObject();
//		try {
//			User user = new User();
//			user.setId("123" + RandomUtil.getRandom(10));
//			user.setName("xw" + RandomUtil.getRandom(10));
//			// 存入一个 user对象
//			redisHelper.set("user".getBytes(),
//					SerializationUtil.serialize(user));
//			// 获取
//			byte[] bs = redisHelper.get("user".getBytes());
//			User desUser = (User) SerializationUtil.deserialize(bs);
//
//			jsonObject = new JSONObject();
//			jsonObject.put("cmd", 0);
//			jsonObject.put("desUser", desUser);
//			// String mygroupuserjson = JSON.toJSONString(krsServerList,
//			// SerializerFeature.WriteDateUseDateFormat);
//			return jsonObject.toString();
//
//		} catch (Exception e) {
//			e.printStackTrace();
//			// logger.error(e.toString(), e);
//			String msgerrorString = e.getMessage();
//			System.out.println(msgerrorString);
//
//			jsonObject = new JSONObject();
//			jsonObject.put("cmd", 500);
//			jsonObject.put("msg", "error:" + msgerrorString);
//			return jsonObject.toString();
//
//		} finally {
//			jsonObject.clear();
//			jsonObject = null;
//		}
//
//	}

	// http://127.0.0.1:8089/dubbo-web/hello/cachdemo.html
	@ResponseBody
	@RequestMapping(value = "/cachdemo.html", method = RequestMethod.GET)
	public String cachDemo(Integer serverid, HttpServletRequest request)
			throws Exception {
		JSONObject jsonObject = new JSONObject();
		try {
//			cacheHelper.addCach("xw", "xw" + RandomUtil.getRandom(10));
//			Object xwObject = cacheHelper.getCach("xw");
			jsonObject = new JSONObject();
			jsonObject.put("cmd", 0);
//			jsonObject.put("xw", xwObject.toString());
			// String mygroupuserjson = JSON.toJSONString(krsServerList,
			// SerializerFeature.WriteDateUseDateFormat);
			return jsonObject.toString();

		} catch (Exception e) {
			e.printStackTrace();
			// logger.error(e.toString(), e);
			String msgerrorString = e.getMessage();
			System.out.println(msgerrorString);

			jsonObject = new JSONObject();
			jsonObject.put("cmd", 500);
			jsonObject.put("msg", "error:" + msgerrorString);
			return jsonObject.toString();

		} finally {
			jsonObject.clear();
			jsonObject = null;
		}

	}

	// http://127.0.0.1:8089/dubbo-web/hello/addnew.html
	@ResponseBody
	@RequestMapping(value = "/addnew.html", method = RequestMethod.GET)
	public String addnew(Integer serverid, HttpServletRequest request)
			throws Exception {
		JSONObject jsonObject = new JSONObject();
		try {

			HelloWorld helloWorld = new HelloWorld();
			helloWorld.setAge(36);
			helloWorld.setName("OM" + RandomUtil.getRandom(10));
			int addcount = 0;
			addcount = helloWorldService.addNew(helloWorld);
			// com.hulk.dao.HelloWorldDao.addNew(helloWorld);
			jsonObject = new JSONObject();
			jsonObject.put("cmd", 0);
			jsonObject.put("addcount", addcount);
			jsonObject.put("helloWorld.id", helloWorld.getId());
			// String mygroupuserjson = JSON.toJSONString(krsServerList,
			// SerializerFeature.WriteDateUseDateFormat);
			return jsonObject.toString();

		} catch (Exception e) {
			e.printStackTrace();
			// logger.error(e.toString(), e);
			String msgerrorString = e.getMessage();
			System.out.println(msgerrorString);

			jsonObject = new JSONObject();
			jsonObject.put("cmd", 500);
			jsonObject.put("msg", "error:" + msgerrorString);
			return jsonObject.toString();

		} finally {
			jsonObject.clear();
			jsonObject = null;
		}

	}

	// http://127.0.0.1:8089/dubbo-web/hello/update.html?serverid=3
	@ResponseBody
	@RequestMapping(value = "/update.html", method = RequestMethod.GET)
	public String update(Integer serverid, HttpServletRequest request)
			throws Exception {
		JSONObject jsonObject = new JSONObject();
		try {
			HelloWorld helloWorld = new HelloWorld();
			helloWorld.setId(serverid);
			helloWorld.setAge(36);
			helloWorld.setName("OM" + RandomUtil.getRandom(10));
			int addcount = 0;
			addcount = helloWorldService.update(helloWorld);
			// com.hulk.dao.HelloWorldDao.addNew(helloWorld);
			jsonObject = new JSONObject();
			jsonObject.put("cmd", 0);
			jsonObject.put("addcount", addcount);
			jsonObject.put("helloWorld", helloWorld);
			// String mygroupuserjson = JSON.toJSONString(krsServerList,
			// SerializerFeature.WriteDateUseDateFormat);
			return jsonObject.toString();

		} catch (Exception e) {
			e.printStackTrace();
			// logger.error(e.toString(), e);
			String msgerrorString = e.getMessage();
			System.out.println(msgerrorString);

			jsonObject = new JSONObject();
			jsonObject.put("cmd", 500);
			jsonObject.put("msg", "error:" + msgerrorString);
			return jsonObject.toString();

		} finally {
			jsonObject.clear();
			jsonObject = null;
		}

	}

	// http://127.0.0.1:8089/dubbo-web/hello/selectOne.html?serverid=1
	@ResponseBody
	@RequestMapping(value = "/selectOne.html", method = RequestMethod.GET)
	public String selectOne(Integer serverid, HttpServletRequest request)
			throws Exception {
		JSONObject jsonObject = new JSONObject();
		try {

			HelloWorld helloWorld = new HelloWorld();
			helloWorld = helloWorldService.selectOne(serverid);
			// com.hulk.dao.HelloWorldDao.addNew(helloWorld);
			jsonObject = new JSONObject();
			jsonObject.put("cmd", 0);
			jsonObject.put("helloWorld", helloWorld);
			// String mygroupuserjson = JSON.toJSONString(krsServerList,
			// SerializerFeature.WriteDateUseDateFormat);
			return jsonObject.toString();

		} catch (Exception e) {
			e.printStackTrace();
			// logger.error(e.toString(), e);
			String msgerrorString = e.getMessage();
			System.out.println(msgerrorString);

			jsonObject = new JSONObject();
			jsonObject.put("cmd", 500);
			jsonObject.put("msg", "error:" + msgerrorString);
			return jsonObject.toString();

		} finally {
			jsonObject.clear();
			jsonObject = null;
		}

	}

	// http://127.0.0.1:8089/dubbo-web/hello/dubbo1.html?serverid=1
	@ResponseBody
	@RequestMapping(value = "/dubbo1.html", method = RequestMethod.GET)
	public String dubboConsumerDemo1(Integer serverid,
			HttpServletRequest request) throws Exception {
		JSONObject jsonObject = new JSONObject();
		try {

			String aa = helloWorldService.hello("aaaaaaaaaaaaaaaaaaaaaaa");
			String bb = helloWorldService.hello("bbbbbbbbbbbbbbbbbbbbb");
			HelloWorld helloWorld = new HelloWorld();
			helloWorld = helloWorldService.selectOne(serverid);
			HelloWorld helloWorld2 = new HelloWorld();
//			helloWorld2 = dubboDemosService.selectOne(serverid);
			
			// com.hulk.dao.HelloWorldDao.addNew(helloWorld);

			// com.hulk.dao.HelloWorldDao.addNew(helloWorld);
			jsonObject = new JSONObject();
			jsonObject.put("cmd", 0);
			jsonObject.put("aa", aa);
			jsonObject.put("bb", bb);
			jsonObject.put("cc", helloWorldService
					.hello("ccccccccccccccccccccccccccccc"));
			jsonObject.put("helloWorld", helloWorld);
			jsonObject.put("helloWorld2", helloWorld2);
			// String mygroupuserjson = JSON.toJSONString(krsServerList,
			// SerializerFeature.WriteDateUseDateFormat);
			return jsonObject.toString();

		} catch (Exception e) {
			e.printStackTrace();
			// logger.error(e.toString(), e);
			String msgerrorString = e.getMessage();
			System.out.println(msgerrorString);

			jsonObject = new JSONObject();
			jsonObject.put("cmd", 500);
			jsonObject.put("msg", "error:" + msgerrorString);
			return jsonObject.toString();

		} finally {
			jsonObject.clear();
			jsonObject = null;
		}

	}

	// http://127.0.0.1:8089/dubbo-web/hello/list.html
	@ResponseBody
	@RequestMapping(value = "/list.html", method = RequestMethod.GET)
	public String list(Integer serverid, HttpServletRequest request)
			throws Exception {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject = new JSONObject();
			jsonObject.put("cmd", 0);
			jsonObject.put("msg", "error:");
			// String mygroupuserjson = JSON.toJSONString(krsServerList,
			// SerializerFeature.WriteDateUseDateFormat);
			return jsonObject.toString();

		} catch (Exception e) {
			e.printStackTrace();
			// logger.error(e.toString(), e);
			String msgerrorString = e.getMessage();
			System.out.println(msgerrorString);

			jsonObject = new JSONObject();
			jsonObject.put("cmd", 500);
			jsonObject.put("msg", "error:" + msgerrorString);
			return jsonObject.toString();

		} finally {
			jsonObject.clear();
			jsonObject = null;
		}

	}
	
	@RequestMapping("/testRedis")
	@SystemControllerLog(description = "获取用户订单列表")
//	@CheckDuplicateSubmission(args = "userId,appType,orderStatus,pageNo", type = "getBasicOrderList")
	public @ResponseBody Object testRedis()
			throws InterruptedException {
		JSONObject jsonObject = new JSONObject();
		Object v = null;
		try {
			v = helloWorldService.testRedis();
			jsonObject.put("v", v);
		} catch (Exception e) {
			jsonObject.put("", e);
			logger.error("获取用户订单列表失败", e);
		}
		return jsonObject;
	}
	
	@RequestMapping("/testInsert")
	@SystemControllerLog(description = "获取用户订单列表")
//	@CheckDuplicateSubmission(args = "userId,appType,orderStatus,pageNo", type = "getBasicOrderList")
	public @ResponseBody Object testInsert()
			throws InterruptedException {
		JSONObject jsonObject = new JSONObject();
		Object v = null;
		try {
			HelloWorld model = new HelloWorld();
			model.setAge(1);
			model.setName("b");
			v = helloWorldService.addNew(model);
			jsonObject.put("v", v);
		} catch (Exception e) {
			jsonObject.put("", e);
			logger.error("获取用户订单列表失败", e);
		}
		return jsonObject;
	}
	
	@RequestMapping("/selectAll")
	@SystemControllerLog(description = "获取用户订单列表")
//	@CheckDuplicateSubmission(args = "userId,appType,orderStatus,pageNo", type = "getBasicOrderList")
	public @ResponseBody Object selectAll()
			throws InterruptedException {
		JSONObject jsonObject = new JSONObject();
		Object v = null;
		try {
			v = helloWorldService.selectAll();
			jsonObject.put("v", v);
		} catch (Exception e) {
			jsonObject.put("", e);
			logger.error("获取用户订单列表失败", e);
		}
		return jsonObject;
	}
	
	@RequestMapping("/testAsyncMethod")
	@SystemControllerLog(description = "testAsyncMethod")
//	@CheckDuplicateSubmission(args = "userId,appType,orderStatus,pageNo", type = "getBasicOrderList")
	public @ResponseBody Object testAsyncMethod(){
		JSONObject jsonObject = new JSONObject();
		Object v = null;
		try {
			helloWorldService.testAsyncMethod();
		} catch (Exception e) {
			jsonObject.put("", e);
			logger.error("testAsyncMethod", e);
		}
		return jsonObject;
	}
	
}
